import { mapState, mapMutations } from 'vuex'
import moment from 'moment'
// 混合过滤器
export const vmfilter = {
  filters: {
    /**
     * 缩略图过滤器
     * @param input
     * @param width must 基本像素 默认会再 x 3，得到一个3倍屏的像素值
     * @param height option 为空取width
     *
     * 助力列表 40
     *
     * @returns {*}
     */
    thumbFilter (input, width, height) {
      if (!input) return ''
      if (!width) return input

      if (!height) height = width
      var realWidth = width * 3 // 几倍屏就乘于多少
      var realHeight = height * 3

      if (input.indexOf('http') === 0 && (input.indexOf('aliyuncs.com') > 0 || input.indexOf('lexiang2018.com') > 0 || input.indexOf('happyshare.com') > 0)) {
        // 阿里云的图片
        // http://happyshare-product.oss-cn-beijing.aliyuncs.com/test/logo.png?x-oss-process=image/resize,m_lfit,h_100,w_100
        input += `?x-oss-process=image/resize,m_fill,limit_0,h_${realWidth},w_${realHeight}`
      } else if (input.indexOf('https://wx.qlogo.cn') === 0) {
        // 微信头像
        input = input.replace(/([0-9]+)$/, width ? width.toString() : '132') // 微信头像
      }
      return input
    },

    moneyFilter (input) {
      // money过滤器 默认为分，需要转换成元
      let result = '0.00'
      if (!/^\d+$/.test(input)) {
        return result
      }
      result = input / 100 + ''
      let aResult = result.split('.')
      if (!aResult[1]) {
        return `${result}.00`
      }
      if (!aResult[1][1]) {
        return `${result}0`
      }
      return result
    },
    moneyFloatFilter (input) {
      let result = '0.00'
      if (!/^\d+$/.test(input)) {
        return result
      }
      result = input / 100 + ''
      let aResult = result.split('.')
      if (!aResult[1]) {
        return `${result}.00`
      }
      if (!aResult[1][1]) {
        return `${result}0`
      }
      return result
    },
    /**
     * 全局时间过滤器
     * @param {*} input
     * @param {时间格式} sFormat
     */
    gDateFilter (input, sFormat) {
      sFormat = sFormat || 'YYYY-MM-DD HH:mm:ss'
      let result = moment(input).format(sFormat)
      if (result === 'Invalid date') {
        return ''
      }
      return result
    }
  }
}
export const vmdata = {
  data () {
    return {
      comShow: false, // 全局显示
      listHeight: '736px' // 混合器获取视窗高度，用户上拉加载
    }
  },
  created () {
  },
  mounted () {

  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    ...mapMutations([
      'USER_INFO'
    ]),
    /**
     * 自定义路由导航
     * @param {路由导航} url
     */
    navTo (url) {
      if (typeof (url) === 'string') {
        // let rdm=Math.ceil(Math.random()*1000);
        // if(url.indexOf("?")>0){
        //   url=`${url}&rdm=${rdm}`;
        // }else{
        //   url=`${url}?rdm=${rdm}`;
        // }
        url = url.replace('undefined', '')
      }
      this.$router.push(url)
    },
    /**
     * 隐藏分享
     */
    hideShareMenu () {
      let _This = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            _This.hideOptionMenu,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', _This.hideOptionMenu)
          document.attachEvent('onWeixinJSBridgeReady', _This.hideOptionMenu)
        }
      } else {
        _This.hideOptionMenu()
      }
    },
    /**
     * 显示分享
     */
    showShareMenu () {
      let _This = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            _This.showOptionMenu,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', _This.showOptionMenu)
          document.attachEvent('onWeixinJSBridgeReady', _This.showOptionMenu)
        }
      } else {
        _This.showOptionMenu()
      }
    },
    /**
     * 获取localstrage
     * @param {*} skey
     * @param {*} isObject
     */
    getLocalStorage (skey, isObject = true) {
      let result = localStorage.getItem(skey) || ''
      if (isObject) {
        result = result || '{}'
        return JSON.parse(result)
      }
      return result
    },
    /**
     * 设置localstorage
     * @param {*} skey
     * @param {*} svalue
     * @param {*} isObject
     */
    setLocalStorage (skey, value = '', isObject = true) {
      if (isObject) {
        value = JSON.stringify(value)
      }
      localStorage.setItem(skey, value)
    },
    hideOptionMenu () {
      WeixinJSBridge.call('hideOptionMenu')
    },
    showOptionMenu () {
      WeixinJSBridge && WeixinJSBridge.call('showOptionMenu')
    }
  },
  destroyed () {
    // this.showShareMenu();
  }
}
/**
 * 加载起始参数，接受前一个页面参数
 */
export const mixinParams = {
  created () {
    let _This = this
    // _This.mixPath = _This.$CONSTANT.localHost + _This.$route.path// 当前路由path
    _This.mixPath = `${location.origin}/#${_This.$route.path}`// 当前路由path
    let oMixParams = _This.$route.params
    for (const key in oMixParams) {
      if (oMixParams.hasOwnProperty(key)) {
        _This[key] = oMixParams[key] || ''
      }
    }
    let oMixQuery = this.$route.query
    for (const key in oMixQuery) {
      if (oMixQuery.hasOwnProperty(key)) {
        _This[key] = oMixQuery[key] || ''
      }
    }
  }

}
